#include <bits/stdc++.h>

#define N 100005

using namespace std;

vector <int> G[N];
vector <int> ne[N];

int dfn[N];
int low[N];
int instack[N];
stack <int> s;
int tim = 0;
int scc_cnt = 0;
int scc[N];

void tarjan(int u){
    low[u] = dfn[u] = ++tim;
    s.push(u);  instack[u] = 1;
    for(int v : G[u]){
        if(!dfn[v]){
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }else if(instack[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        scc_cnt++;
        while(!s.empty() && s.top() != u){
            int v = s.top();
            s.pop();
            scc[v] = scc_cnt;
            instack[v] = 0;
        }
        s.pop();
        scc[u] = scc_cnt;
        instack[u] = 0;
    }
}

void rebuild(){

}

int main(){
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int u, v;
        scanf("%d %d", &u, &v);
        G[u].push_back(v);
    }
    for(int i = 1; i <= n; i++){
        if(!dfn[i]) tarjan(i);
    }
    rebuild();
    return 0;
}